<?php

class DefaultController extends AController
{
	
	public function actionIndex()
	{
		$this->render('index');
	}
	
	public function actionAjaxCatalog(){
	    $type = 'brand';
	    $id = $this->getModel($model, $type);
		$data = Brand::model()->findAll(array('order'=>'title desc'));

		$tmp = array();
		foreach($data as $e){
			$c = count($tmp);
			$tmp[$c] = array('id'=>$e->id, 'title'=>$e->title);
			if($e->id == $id)
				$tmp[$c]['selected'] = true;
		}
		
		echo $data = CJavaScript::jsonEncode(array('type'=>$type, 'data'=>$tmp));
	
		Yii::app()->end();
	}
	
	public function actionAjaxBrand(){
	    $type = 'family';
		$id = $this->getModel($model, $type);
		$data = Family::model()->findAll('brand_id=:brand_id', array(':brand_id'=>(int) $_POST['brand_id']));
	    	    
		if(count($data)<=0) {
			$type = 'model';
			$data = Car::model()->findAll('brand_id=:brand_id', array(':brand_id'=>(int) $_POST['brand_id']));
		}
		
		$tmp = array();
		foreach($data as $e){
			$c = count($tmp);
			$tmp[$c] = array('id'=>$e->id, 'title'=>$e->title);
			if($e->id == $id)
				$tmp[$c]['selected'] = true;
		}

		echo $data = CJavaScript::jsonEncode(array('type'=>$type, 'data'=>$tmp));

		Yii::app()->end();	
	}
	
	public function actionAjaxFamily(){
		$type = 'model';
		$id = $this->getModel($model, $type);
		$data = Car::model()->findAll('family_id=:family_id', array(':family_id'=>(int) $_POST['family_id']));
		
		$tmp = array();
		foreach($data as $e){
			$c = count($tmp);
			$tmp[$c] = array('id'=>$e->id, 'title'=>$e->title);
			if($e->id == $id)
				$tmp[$c]['selected'] = true;
		}
		
		echo $data = CJavaScript::jsonEncode(array('type'=>$type, 'data'=>$tmp));

		Yii::app()->end();	
	}
	
	public function actionAjaxModel(){
		$type = 'modification';
		$id = $this->getModel($model, $type);
		$data = Modification::model()->findAll('model_id=:model_id', array(':model_id'=>(int) $_POST['model_id']));
		
		$tmp = array();
		foreach($data as $e){
			$c = count($tmp);
			$tmp[$c] = array('id'=>$e->id, 'title'=>$e->title);
			if($e->id == $id)
				$tmp[$c]['selected'] = true;
		}
		
		echo $data = CJavaScript::jsonEncode(array('type'=>$type, 'data'=>$tmp));

		Yii::app()->end();
	}
	
	private function getModel(&$model, $type){
		$r = Yii::app()->request;
		$brand = $r->getQuery('Brand');
		$family = $r->getQuery('Family');
		$model = $r->getQuery('Car');
		$modification = $r->getQuery('Modification');
		
		if($brand && $brand > 0) {
			$model = Brand::model()->findbyPk($brand);
			switch($type){
				case 'brand':
					return $model->id;
				break;
			}
		}

		if($family && $family > 0) {
			$model = Family::model()->findbyPk($family);
			switch($type){
				case 'brand':
					return $model->brand_id;
				break;
				case 'family':
					return $model->id;
				break;
			}
		}

		if($model && $model > 0) {
			$model = Car::model()->findbyPk($model);
			switch($type){
				case 'brand':
					return $model->brand_id;
				break;
				case 'family':
					return $model->family_id;
				break;
				case 'model':
					return $model->id;
				break;
			}
		}
			
		if($modification && $modification > 0) {
			$model = Modification::model()->findbyPk($modification);
			switch($type){
				case 'brand':
					return $model->model->brand_id;
				break;
				case 'family':
					return $model->model->family_id;
				break;
				case 'model':
					return $model->model_id;
				break;
				case 'modification':
					return $model->id;
				break;
			}
		}
		
		return null;
	}
	
	/*****************************************/
	
	
	public function actionAjaxCat(){
		if (!Yii::app()->request->isAjaxRequest) {
			$this->redirect('index');
		}
		
        if(isset($_GET['root']) && $_GET['root'] == 'source')
        	echo $this->getBrands();
		elseif(isset($_GET['root']))
			echo $this->getChilds($_GET['root']);
	}
	
	private function getBrands(){
        $data = Brand::model()->with('families', 'models')->findAll();

        $tmp = array();
        foreach($data as $model){
			$c = count($tmp);
			$tmp[$c] = $this->makeText($model);
			if(count($model->families) > 0 || count($model->models) > 0)
				$tmp[$c]['hasChildren'] = true;
		}
		
		return CJavaScript::jsonEncode(array(array(
				'text'=>'Автокаталог',
				'expanded'=>true,
				'classes'=>'root',
				'children'=>$tmp,
			)));
	}
	
	private function getChilds($node){
		list($id, $type) = split(':', $node, 2);
		switch($type){
		case 'brand':
			$brand = Brand::model()->findByPK($id);
			$tmp = array();
			if(count($brand->families) > 0) {
				foreach($brand->families as $model){
					$c = count($tmp);
					$tmp[$c] = $this->makeText($model);
					if(count($model->models)>0)
						$tmp[$c]['hasChildren'] = true;
				}
			} elseif(count($brand->models) > 0) {
				foreach($brand->models as $model){
					$c = count($tmp);
					$tmp[$c] = $this->makeText($model);
					if(count($model->modifications)>0)
						$tmp[$c]['hasChildren'] = true;
				}
			}
			
			return CJavaScript::jsonEncode($tmp);
		break;
		case 'family':
			$family = Family::model()->findByPK($id);
			$tmp = array();
			foreach($family->models as $model){
				$c = count($tmp);
				$tmp[$c] = $this->makeText($model);
				if(count($model->modifications) > 0)
					$tmp[$c]['hasChildren'] = true;
			}
			
			return CJavaScript::jsonEncode($tmp);
		break;
		case 'car':
			$car = Car::model()->findByPK($id);
			$tmp = array();
			foreach($car->modifications as $model){
				$c = count($tmp);
				$tmp[$c] = $this->makeText($model);
			}
			
			return CJavaScript::jsonEncode($tmp);
		break;
		default:
			
			return CJavaScript::jsonEncode(array());
		break;
		}
	}
	
	private function makeText($model){
		return array(
			'text' => '<a href="#" class="href-like-text catalog-selector '.strtolower(get_class($model)).'" rel="'.$model->id.':'.strtolower(get_class($model)).'">'.$model->title.'</a>',
			'id' => $model->id.':'.strtolower(get_class($model)),
		);
	}
}